﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.EducationAndTraining
{
    public class StudentRegistries : DetailView<studentregistry>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<studentregistry> EntityList;
        private new studentregistry Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<studentregistry>

        public override List<studentregistry> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.studentregistries.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<studentregistry> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.studentregistries.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override studentregistry GetEntity()
        {
            _context = new project5188Entities();
            Entity = new studentregistry();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTostudentregistries(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<studentregistry> GetEntityListByParameters(string studentName, string tcNoText, DateTime? birthDate)
        {
            _context = new project5188Entities();

            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            if (!string.IsNullOrEmpty(studentName) && !string.IsNullOrWhiteSpace(studentName))
            {
                dynamicString += " And it.e_NameSurname = @StudentName";
                parameterList.Add(new ObjectParameter("StudentName", studentName));
            }

            if (!String.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                dynamicString += " And it.f_TCNo = @TCNo";
                parameterList.Add(new ObjectParameter("TCNo", Convert.ToInt32(tcNoText)));
            }

            if (birthDate != null)
            {
                dynamicString += " And it.j_BirthDate = @BirthDate";
                parameterList.Add(new ObjectParameter("BirthDate", birthDate));
            }

            EntityList = _context.studentregistries.Where(dynamicString, parameterList.ToArray()).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
